<?php
namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Session;
use think\Db;

use app\index\model\User as UserModel;

class User extends Controller
{
	public function _initialize()
    {
    	// 获取用户信息 $indexUserInfo['indexUserInfo']
        $indexUserInfo = Session::get('indexUserInfo');
        if ($indexUserInfo == null) {
            $this->redirect('/index/index/login.html');
        }
    }
	
    public function index()
    {
    	
    	$this->assign("title", "我的资料");
        return $this->fetch();   	
    }

	 public function mypingjiao()
    {
    	
    	$indexUserInfo = Session::get('indexUserInfo');
        if ($indexUserInfo == null) {
            $this->redirect('/index/index/login.html');
        }
    	$mypingjiaosql = "
    		SELECT 
				etype.`name` AS 'ename',
				teacher.`name` AS 'tname',
				erecor.score AS 'score'
			FROM (
				oes_erecordnew erecor LEFT JOIN oes_etype etype ON etype.id = erecor.etype_id
			) LEFT JOIN oes_emp teacher ON teacher.id = erecor.teacher_id
			  WHERE erecor.euser_id = ?
    	";
		
		$mypingjiaosql2 = "
    		SELECT 
				etype.`name` AS 'ename',
				etype.`score` AS 'tscore',
				teacher.`name` AS 'tname',
				COUNT(erecor.etype_id) AS 'grup',
				sum(erecor.score) AS 'score'
			FROM (
				oes_erecordnew erecor LEFT JOIN oes_etype etype ON etype.id = erecor.etype_id
			) LEFT JOIN oes_emp teacher ON teacher.id = erecor.teacher_id
			  WHERE erecor.teacher_id = ?
			  GROUP BY erecor.etype_id
    	";
	
		if($indexUserInfo['type'] == 2){
			$mypingjiaoList = Db::query($mypingjiaosql, [$indexUserInfo['sysuser_id']]);
	    	//dump($mypingjiaoList);
			$this->assign("tab1count",count($mypingjiaoList));
		}else{
			$mypingjiaoList = Db::query($mypingjiaosql, [$indexUserInfo['sysuser_id']]);
	    	//dump($mypingjiaoList);
			$this->assign("tab1count",count($mypingjiaoList));
			//dump(tab2content);
			$userInfo = Db::table('oes_emp')->where('sysuser_id='.$indexUserInfo['sysuser_id'])->find();
	        if ($userInfo!=null) {
	        	
	        	$mypingjiaoList2 = Db::query($mypingjiaosql2, [$userInfo['id']]);
	        	$sumscore = 0;
	        	$sumscore1 = 0;
	        	foreach ($mypingjiaoList2 as &$item) {
	        		//{$vo.score/$vo.grup*$vo.tscore}
	        	   //$sumscore = $item['score'] / $item['grup'] * $item['tscore'] + $sumscore;
	        	   $sumscore = $item['score'] + $sumscore;
		        }
				//$this->assign("sumscore",$sumscore);
				
				foreach ($mypingjiaoList2 as &$item1) {
					$item1['sumscore'] = $sumscore * $item1['tscore'];
	        	   $sumscore1 = $sumscore * $item1['tscore'] + $sumscore1;
		        }
				$this->assign("sumscore1",$sumscore1);
	        }else{
	        	$mypingjiaoList2 = null;
	        }
			
			$this->assign("tab2content",$mypingjiaoList2);
			//dump($mypingjiaoList2);
		}
		
		
    	$this->assign("title", "我的评教");
        return $this->fetch();   	
    }
	
	/**
	 * 获取我的评教信息
	 */
	public function mypingjiaoList()
    {
    	$indexUserInfo = Session::get('indexUserInfo');
        if ($indexUserInfo == null) {
            $this->redirect('/index/index/login.html');
        }
    	$mypingjiaosql = "
    		SELECT 
			etype.`name` AS 'ename',
			teacher.`name` AS 'tname',
			erecor.score AS 'score'
			FROM (
				oes_erecordnew erecor LEFT JOIN oes_etype etype ON etype.id = erecor.etype_id
			) LEFT JOIN oes_emp teacher ON teacher.id = erecor.teacher_id
			WHERE erecor.euser_id = ?
    	";
		
		$mypingjiaoList = Db::query($mypingjiaosql, [$indexUserInfo['sysuser_id']]);
    	//dump($mypingjiaoList);
        return json(["data"=>$mypingjiaoList]);
    }
	
	 /**
     * 修改个人资料
     * @param Request $request
     * @return \think\response\Json
     */
    public function updateUserInfoPost(Request $request) {
        // 获取用户信息 $indexUserInfo['indexUserInfo']
        $indexUserInfo = Session::get('indexUserInfo');
        if ($indexUserInfo == null) {
            $this->redirect('/index/index/login.html');
        }
        // 获取前端传的参数
        $data = $request->param();
		
        if ($data == null) {
            return json(['success'=>false, "msg"=>'修改资料填写有误']);
        }
		
		$whereContent['sysuser_id'] = $indexUserInfo['sysuser_id'];
		try{
			
			if($indexUserInfo['type'] !=2){//查当前登录用户不为学生的基本信息
				
				$userInfo = Db::table('oes_emp')->where($whereContent)->update($data);
			}else{
				$userInfo = Db::table('oes_student')->where($whereContent)->update($data);
			}
			
		} catch (\Exception $e) {
			return json(['success'=>false, "msg"=>'网络错误']);
		}
       
        if ($userInfo!=null) {
        	if($indexUserInfo['type'] !=2){//查当前登录用户不为学生的基本信息
				
				$indexUserInfo1 = Db::table('oes_emp')->where($whereContent)->find();
			}else{
				$indexUserInfo1 = Db::table('oes_student')->where($whereContent)->find();
			}
            
			$indexUserInfo1['type'] = $indexUserInfo['type'];
			$indexUserInfo1['username'] = $indexUserInfo['username'];
			$indexUserInfo1['classcode'] = $indexUserInfo['classcode'];
			$indexUserInfo1['usererecord'] = $indexUserInfo['usererecord'];
			if($indexUserInfo['type'] != 2){
				$indexUserInfo1['userberecord'] = $indexUserInfo['userberecord'];
			}
			
            Session::set('indexUserInfo', $indexUserInfo1);

        	return json(['success'=>true, "msg"=>'修改个人资料成功', 'obj'=>$indexUserInfo1]);
        } else {
            return json(['success'=>false, "msg"=>'当前网络不稳，修改个人资料失败了']);
        }
    }

 	/**
     * 修改密码
     * @param Request $request
     * @return \think\response\Json
     */
	public function updateUserPasswordPost(Request $request){
		// 获取用户信息 $indexUserInfo['indexUserInfo']
        $indexUserInfo = Session::get('indexUserInfo');
        if ($indexUserInfo == null) {
            $this->redirect('/index/index/login.html');
        }
        // 获取前端传的参数
        $data = $request->only(['oldpassword','password']);
		 if ($data == null) {
            return json(['success'=>false, "msg"=>'提交数据为空了']);
        }
		
		$data1['password'] = MD5($data['oldpassword'].'OES');
		$data1['username'] = $indexUserInfo['username'];
        $userInfo = Db::table('user')->where($data1)->find();
		if($userInfo > 0){
			$whereContent['id'] = $indexUserInfo['sysuser_id'];
			$data2['password'] = MD5($data['password'].'OES');
			try{
				$userInfo = Db::table('user')->where($whereContent)->update($data2);
			} catch (\Exception $e) {
				return json(['success'=>false, "msg"=>'网络错误']);
			}
	       
	        if ($userInfo!=null) {
	        	return json(['success'=>true, "msg"=>'修改个人密码成功', 'obj'=>$userInfo]);
	        } else {
	            return json(['success'=>false, "msg"=>'当前网络不稳，修改个人密码失败了']);
	        }
		}else{
			return json(['success'=>false, "msg"=>'原始密码输入错误']);
		}
		
		
	}

}
